package com.mingweicai.intergration.thread;

import java.util.concurrent.Exchanger;

public class ExchangerTest {

  public static void main(String args[]){

    Exchanger<String> exchanger = new Exchanger<>();

    new Thread(() -> {
      try {
        String data = "Thread1 的数据";
        String receivedData = exchanger.exchange(data);
        System.out.println("线程1收到来自线程2的数据 " + receivedData);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }).start();

    new Thread(() -> {
      try {
        String data = "Thread2 的数据";
        String receivedData = exchanger.exchange(data);
        System.out.println("线程2收到来自线程1的数据 " + receivedData);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }).start();
  }

}
